﻿using System.ComponentModel;
using System.Linq;
using System.Collections.ObjectModel;
using WpfPostgresql.Model;

namespace WpfPostgresql.ViewModel
{
    internal class PosilkasListViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Databases.Posilka> Posilkas { get; set; }

        private string _selectedVoditelName;
        public string SelectedVoditelName
        {
            get { return _selectedVoditelName; }
            set { _selectedVoditelName = value; RaisePropertyChanged("SelectedVoditelName"); }
        }

        private string _selectedMagazName;
        public string SelectedMagazName
        {
            get { return _selectedMagazName; }
            set { _selectedMagazName = value; RaisePropertyChanged("SelectedMagazName"); }
        }


        private ObservableCollection<Databases.Tovar> _tovarFromPosilka;
        public ObservableCollection<Databases.Tovar> TovarFromPosilka
        {
            get { return _tovarFromPosilka; }
            set { _tovarFromPosilka = value; RaisePropertyChanged("TovarFromPosilka"); }
        
        }


        private Databases.Posilka _selectedPosilka;
        public Databases.Posilka SelectedPosilka
        {
            get { return _selectedPosilka; }
            set { _selectedPosilka = value; RaisePropertyChanged("SelectedPosilka"); Referesh(); }
        }
            
        public PosilkasListViewModel()
        {
            Posilkas = new ObservableCollection<Databases.Posilka>(Databases.GetPosilks());
            if (Posilkas.Count!=0)
            {
                _selectedPosilka = Posilkas.First();
                Referesh();
            }
            
        }

        public void Referesh()
        {
            TovarFromPosilka = new ObservableCollection<Databases.Tovar>(Databases.GetTovarListByPosilkaId(SelectedPosilka.ID));
            SelectedVoditelName = Databases.GetVoditelByPosilkaId(SelectedPosilka.ID).Name;
            var tempMagaz = Databases.GetMagazinlByPosilkaId(SelectedPosilka.ID);
            SelectedMagazName = tempMagaz.Name + " " + tempMagaz.Addres;
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
